from typing import Annotated,Literal

import uvicorn
from fastapi import FastAPI, Path, Query, Body
from fastapi.openapi.docs import get_swagger_ui_html, get_swagger_ui_oauth2_redirect_html, get_redoc_html
from starlette.staticfiles import StaticFiles
from pydantic import BaseModel, Field, HttpUrl

# 创建FastAPI实例，禁用掉FastAPI会自动生成Swagger UI和Redoc文档
app = FastAPI(docs_url=None, redoc_url=None)

# 静态文件挂载（路由与静态文件映射）
app.mount("/static", StaticFiles(directory="../statics"), name="static")


# 重新提供一个Swagger UI，相当于我们自己重写了这么一个路由
@app.get("/docs", include_in_schema=False)
def custom_swagger_ui():
    return get_swagger_ui_html(
        openapi_url = "/openapi.json",
        title = "Swagger UI for FastAPI",
        swagger_js_url="/static/swagger-ui-bundle.min.js",
        swagger_css_url="/static/swagger-ui.min.css"
    )
@app.get(app.swagger_ui_oauth2_redirect_url, include_in_schema=False)
async def swagger_ui_redirect():
    return get_swagger_ui_oauth2_redirect_html()

# 这里与Swagger UI一样，重新提供Redoc
@app.get("/redoc", include_in_schema=False)
def custom_redoc():
    return get_redoc_html(
        openapi_url = "/openapi.json",
        title = "Redoc for FastAPI",
        redoc_js_url="/static/redoc.standalone.js"
    )

# class Item(BaseModel):
#     # 这里的...是第一个参数，表示必填项，可以省略不写
#     name: str = Field(...,examples=["This is item name"])
#     description: str | None = Field(default=None,examples=["This is description"])
#     price: float = Field(...,examples=[100.0])
#     tax: float | None = Field(default=None,examples=[10.0])
#
#     # # 添加额外信息
#     # model_config = {
#     #     "json_schema_extra": {
#     #         "examples": [
#     #             {
#     #                 "name": "item's name",
#     #                 "description": "description of the item",
#     #                 "price": 3999.99,
#     #                 "tax": 3.2
#     #             }
#     #         ]
#     #     }
#     # }

class Item(BaseModel):
    name: str
    description: str | None = None
    price: float
    tax: float | None = None

@app.put("items/{item_id}")
async def update_item(item_id: int,
                      item: Annotated[Item, Body(
                          examples=[
                              {
                                  "name": "这里是名称",
                                  "description": "这里是描述信息",
                                  "price": 9.99,
                                  "tax": 3.2
                              }
                          ]
                      )]):
    return {"item_id": item_id, "item": item}


if __name__ == '__main__':
    app.debug = True
    uvicorn.run("main:app", host="127.0.0.1", port=8010, reload=True)

